/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.appweb;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.responseReturn.entity.ResultObject;
import com.jeesite.common.responseReturn.utils.CommonUtils;
import com.jeesite.common.responseReturn.web.Render;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtAddr;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtQuan;
import com.jeesite.modules.goods.entity.ZtQuanself;
import com.jeesite.modules.goods.service.ZtQuanService;
import com.jeesite.modules.goods.service.ZtQuanselfService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${frontPath}/ztQuanself")
public class AppZtQuanselfController extends BaseController {

	@Autowired
	private ZtQuanselfService ztQuanselfService;

	@Autowired
	private ZtQuanService ztQuanService;

	@Autowired
	private MyProps myProps;

	/**
	 * 现金券
	 */

	@RequestMapping(value = "listXJQuan")
	@ResponseBody
	public void listData(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		ResultObject res = new ResultObject();

		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);

		String adminid = jo.getString("adminid");

		ZtQuanself ztQuanself = new ZtQuanself();
		ZtQuan ztQuan = new ZtQuan();
		ZtAdmin ztAdmin = new ZtAdmin();
		ztAdmin.setId(adminid);
		ztQuanself.setZtAdmin(ztAdmin);
		ztQuanself.setUsestatus("0");
		ztQuan.setType("1");
		ztQuanself.setZtQuan(ztQuan);
		List<ZtQuanself> page = ztQuanselfService.findList(ztQuanself);
		List<ZtQuanself> zqsList=new ArrayList<ZtQuanself>();
		
		if (page != null && page.size() > 0) {
			for (ZtQuanself zd : page) {
				
				ZtQuan ztQ = zd.getZtQuan();
				
				if(ztQ.getToTime()!=null && ztQ.getToTime().getTime()<System.currentTimeMillis()){
					continue;
				}
				
				ztQ.setImg(myProps.getServerPath()+ztQ.getImg());
				
				zqsList.add(zd);
			}
		}

		res.setResult(200L);
		res.setData(zqsList);
		Render.renderString(response, res);

		

	}

	/**
	 * 我的优惠券
	 */

	@RequestMapping(value = "listQuan")
	@ResponseBody
	public void listQuan(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		ResultObject res = new ResultObject();

		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);

		String adminid = jo.getString("adminid");
		
		String usestatus = jo.getString("usestatus");

		ZtQuanself ztQuanself = new ZtQuanself();
		ZtQuan ztQuan = new ZtQuan();
		ZtAdmin ztAdmin = new ZtAdmin();
		ztAdmin.setId(adminid);
		ztQuanself.setZtAdmin(ztAdmin);
		
		
		
		
		if("0".equals(usestatus)){
			
			ztQuanself.setUsestatus(usestatus);//未使用    1已使用
			ztQuanself.setZtQuan(ztQuan);
			List<ZtQuanself> page = ztQuanselfService.findList(ztQuanself);
			
		List<ZtQuanself> zqsList=new ArrayList<ZtQuanself>();
		
		if (page != null && page.size() > 0) {
			for (ZtQuanself zd : page) {
				
				ZtQuan ztQ = zd.getZtQuan();
				
				int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQ.getToTime());
				if(comptime==-1&&("0".equals(ztQ.getUsestatus()))&&ztQ.getOverCount()>0){//券有效
					ztQ.setImg(myProps.getServerPath()+ztQ.getImg());
					zqsList.add(zd);
				}

			}
		}
		res.setData(zqsList);
		}else if("1".equals(usestatus)){
			
			ztQuanself.setUsestatus(usestatus);//未使用    1已使用
			ztQuanself.setZtQuan(ztQuan);
			List<ZtQuanself> page = ztQuanselfService.findList(ztQuanself);
			
		
			res.setData(page);
		}else if("2".equals(usestatus)){
			
			ztQuanself.setUsestatus(usestatus);//未使用    1已使用
			ztQuanself.setZtQuan(ztQuan);
			List<ZtQuanself> page = ztQuanselfService.findList(ztQuanself);
			
			
			List<ZtQuanself> zqsList=new ArrayList<ZtQuanself>();
			
			if (page != null && page.size() > 0) {
				for (ZtQuanself zd : page) {
					
					ZtQuan ztQ = zd.getZtQuan();
					
					int comptime=com.jeesite.modules.pay.totalPay.util.DateUtils.compare_date(new Date(), ztQ.getToTime());
					if(comptime==-1&&("0".equals(ztQ.getUsestatus()))&&ztQ.getOverCount()>0){//券有效
						
					}else{
						ztQ.setImg(myProps.getServerPath()+ztQ.getImg());
						zqsList.add(zd);
					}

				}
			}
			res.setData(zqsList);
			
		}
		
		
		res.setResult(200L);	
		Render.renderString(response, res);
	}

	/**
	 * 优惠券
	 */

	@RequestMapping(value = "save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response,
			Model model) {
		ResultObject res = new ResultObject();

		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);

		String adminid = jo.getString("adminid");
		String quanid = jo.getString("quanid");
		
		ZtAdmin ztAdmin = new ZtAdmin();
		ztAdmin.setId(adminid);
		
		ZtQuanself ztQuanself = new ZtQuanself();

		ztQuanself.setCreateDate(new Date());
		ZtQuan ztQuan=new ZtQuan();
		ztQuan.setId(quanid);
		ztQuan=ztQuanService.get(ztQuan);
		ztQuanself.setZtQuan(ztQuan);
		ztQuanself.setZtAdmin(ztAdmin);
		ztQuanself.setCreateDate(new Date());
		
		
		
		ztQuanselfService.save(ztQuanself);//保存领取券码
		
		ztQuan.setOverCount(ztQuan.getOverCount()-1);//剩余
		ztQuan.setTotal(ztQuan.getTotal()+1);//已领取
		ztQuanService.save(ztQuan);//更新券数

		res.setResult(200L);
		res.setData(ztQuan);
		Render.renderString(response, res);

	}

}